clean__adm__main<-function(years){
  Sys.setlocale(locale="Romanian")
  setwd(wd_code)
  eval(parse('clean__adm__clean_names.R', encoding = 'UTF-8'))
  eval(parse('clean__adm__clean_ms.R', encoding = 'UTF-8'))
  eval(parse('clean__adm__clean_liceu_repartizat.R', encoding = 'UTF-8'))
  
  
  for (j in years){
    print(j)
    Sys.setlocale(locale="English")
    admission_file<-paste("admitere_",j,".csv",sep="")
    
    #2010 data had missing HS; using data provided by Andreea Mitrut, which is in a slightly different format
    if (j==2010){
      setwd(wd_data_raw)
      data_adm_raw<-read.csv(admission_file,sep = ";",	encoding="UTF-8", stringsAsFactors=FALSE,quote="")

      
    }else {
      setwd(wd_data_raw)
      data_adm_raw<-read.csv(admission_file,sep = ";",	encoding="UTF-8", stringsAsFactors=FALSE)
    }
    #data_adm_raw <- data_adm_raw %>% mutate(id = j*1000000+row_number())
    data_adm_raw <- mutate(data_adm_raw, id = rownames(data_adm_raw))
    data_adm_raw$judet<-as.character(data_adm_raw$judet)
    data_adm_raw[grepl("BUCURESTI",data_adm_raw$judet),]$judet<-"BUCURESTI"
    data_adm_raw$judet<-ifelse(data_adm_raw$judet=='MUNICIPIUL BUCUREȘTI','BUCURESTI',data_adm_raw$judet)
    data_adm_raw$judet<-ifelse(data_adm_raw$judet=='-',NA,data_adm_raw$judet)
    data_adm_raw$judet<-ifelse(data_adm_raw$judet=='',NA,data_adm_raw$judet)
    data_adm_raw$judet<-trimws(data_adm_raw$judet)
    
   #  char_name_adm<-sort(unique(unlist(unique(strsplit(as.character(data_adm_raw$nume), "")))))
   #  char_school_adm<-sort(unique(unlist(unique(strsplit(as.character(data_adm_raw$scoala_de_provenienta), "")))))
   # 
   # char_school_adm<-sort(unique(unlist(unique(strsplit(as.character(data_adm_raw$liceu_repartizat), "")))))
   #  
   #  test<-data_adm_raw %>% filter(grepl("CROAT",liceu_repartizat)) %>% distinct(scoala_de_provenienta)
    
    #the 2004 exam did not have Enlgish, and there is no info on grades 5-8 average (though this can be backed out)
    #so columns are not in the right order
    if (j==2004){
      data_adm_raw[,c(15,16)]<-data_adm_raw[,c(13:14)]
      data_adm_raw[,c(7:13)]<-data_adm_raw[,c(6:12)]
      data_adm_raw$media_de_absolvire<-NA
      
      data_adm_raw<-data_adm_raw %>%
        separate(liceu_repartizat, c("liceu_repartizat", "tip_liceu"), "(?=(Scoala))|(?=(Real/))|(?=(Uman/))|(?=(Resurse))|(?=(Tehnic/))|(?=(Servicii/))",extra="merge")
      
    }

    
    Sys.setlocale(locale="Romanian")
    
    ########### Clean Data #################
    data_adm_raw$nume<-clean__adm__clean_names(data_adm_raw,j)
    print("adm name cleaned")
    data_adm_raw<-clean__adm__clean_ms(data_adm_raw,j)
    print("ms cleaned")
    data_adm_raw<-clean__adm__clean_liceu_repartizat(data_adm_raw,j)
    print("liceu repartizat adm cleaned")
    
    ##### fix issue with some ms-hs being in the wrong columns (inverted); 'VIII' is a marker for MS; should not be in HS col
    # data_adm_raw$temp_school<-data_adm_raw$liceu_repartizat
    # data_adm_raw[grepl("VIII",data_adm_raw$liceu_repartizat),]$temp_school<-data_adm_raw[grepl("VIII",data_adm_raw$liceu_repartizat),]$liceu_repartizat
    # data_adm_raw[grepl("VIII",data_adm_raw$liceu_repartizat),]$liceu_repartizat<-data_adm_raw[grepl("VIII",data_adm_raw$liceu_repartizat),]$scoala_de_provenienta
    # data_adm_raw[grepl("VIII",data_adm_raw$liceu_repartizat),]$scoala_de_provenienta<-data_adm_raw[grepl("VIII",data_adm_raw$liceu_repartizat),]$temp_school
    # data_adm_raw<-data_adm_raw %>% select(-temp_school)
    #######
    #edit: turns out this was okay; some students got assigned to middle schools
    #who would later turn into HS's
    temp<-data_adm_raw[grepl("VIII",data_adm_raw$liceu_repartizat),]
    sum(grepl("VIII",data_adm_raw$liceu_repartizat))
    
    admission_file_final<-paste("data_adm_raw",j,sep="")
    

    setwd(wd_data_intermediate)
    saveRDS(data_adm_raw, file = paste(admission_file_final,".rds",sep=""))
    #data_adm_raw<-readRDS(paste(admission_file_final,".rds",sep=""))
  }
}
